import 'package:catguard/views/dialog_manager.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../../config/config.dart';
import '../../generated/assets.dart';
import '../../routers/routers.dart';
import '../../views/common_widget.dart';
import 'logic.dart';

class FamilyManagerPage extends StatelessWidget {
  FamilyManagerPage({super.key});

  final logic = Get.find<FamilyManagerLogic>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Config.colorBg,
      resizeToAvoidBottomInset: false,
      body: Stack(
        children: [
          Column(
            children: [
              CommonWidget.commonAppBar("家庭管理"),
              //菜单
              Container(
                width: double.infinity,
                margin: EdgeInsets.only(top: 30.w, left: 35.w, right: 35.w),
                padding: EdgeInsets.symmetric(horizontal: 35.w, vertical: 4.w),
                decoration: BoxDecoration(
                  color: Config.colorBg2,
                  borderRadius: BorderRadius.circular(Config.roundBig.w),
                ),
                child: ListView(
                  padding: const EdgeInsets.symmetric(vertical: 0),
                  scrollDirection: Axis.vertical,
                  shrinkWrap: true,
                  children: [
                    menuItem("家庭名称", "Sam1的家庭", true, () {
                      DialogManager.showEditDialog("家庭名称", (text){
                        EasyLoading.showToast("修改后的家庭名称$text");
                      },hint: "Sam1的家庭");
                    }),
                    CommonWidget.commonDivider(),
                    menuItem("家庭设备", "3台设备", false, () {}),
                  ],
                ),
              ),
              Container(
                margin: EdgeInsets.only(top: 30.w),
                padding: EdgeInsets.symmetric(horizontal: 64.w),
                alignment: Alignment.centerLeft,
                child: Text(
                  "家庭成员",
                  style: TextStyle(color: Config.colorText3, fontSize: Config.textSizeContent.sp),
                ),
              ),
              //成员列表
              Container(
                width: double.infinity,
                margin: EdgeInsets.only(top: 20.w, left: 35.w, right: 35.w),
                padding: EdgeInsets.symmetric(horizontal: 35.w, vertical: 4.w),
                decoration: BoxDecoration(
                  color: Config.colorBg2,
                  borderRadius: BorderRadius.circular(Config.roundBig.w),
                ),
                child: ListView(
                    padding: const EdgeInsets.symmetric(vertical: 0),
                    scrollDirection: Axis.vertical,
                    shrinkWrap: true,
                    children: List.generate(logic.familyMember.length, (index) {
                      return familyItem(index, () {});
                    })),
              ),
            ],
          ),
          Positioned(
              bottom: 120.w,
              child: Container(
                width: Get.width,
                alignment: Alignment.center,
                child: CommonWidget.commonButton("添加家庭成员", () {
                  Get.toNamed(Routes.familyShare);
                },radius: 60.w,width: 580.w),
              )),
        ],
      ),
    );
  }

  menuItem(String title, String? subTitle, bool isRightArrow, Function() onTap) {
    return InkWell(
      onTap: onTap,
      child: SizedBox(
        height: 114.w,
        child: Row(
          children: [
            Text(
              title,
              style: TextStyle(color: Config.colorText1, fontSize: Config.textSizeContent.sp),
            ),
            Expanded(
                child: Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                Visibility(
                  visible: subTitle != null,
                  child: Text(
                    subTitle ?? "",
                    style: TextStyle(color: Config.colorText3, fontSize: Config.textSizeContent.sp),
                  ),
                ),
                Visibility(
                  visible: isRightArrow,
                  child: Icon(
                    Icons.arrow_forward_ios_rounded,
                    color: Config.colorText3.withOpacity(0.7),
                    size: 32.w,
                  ),
                ),
              ],
            ))
          ],
        ),
      ),
    );
  }

  familyItem(int index, Function() onTap) {
    return InkWell(
      onTap: onTap,
      child: Column(
        children: [
          SizedBox(
            height: 114.w,
            child: Row(
              children: [
                Container(
                    width: 66.w,
                    height: 66.w,
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(33.w),
                      border: Border.all(color: Config.colorMain, width: 1.w),
                    ),
                    clipBehavior: Clip.antiAlias,
                    child: ClipRRect(
                      borderRadius: BorderRadius.circular(33.w),
                      child: Image.asset(
                        Assets.imagesLauncher,
                        fit: BoxFit.cover,
                      ),
                    )),
                SizedBox(width: 20.w),
                Text(
                  logic.familyMember[index],
                  style: TextStyle(color: Config.colorText1, fontSize: Config.textSizeContent.sp),
                ),
                Expanded(
                    child: Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    Visibility(
                      visible: index == 0,
                      child: Text(
                        "管理员",
                        style:
                            TextStyle(color: Config.colorText2, fontSize: Config.textSizeInfo.sp),
                      ),
                    ),
                    Visibility(
                      visible: index != 0,
                      child: GestureDetector(
                        onTap: (){
                          DialogManager.showDialog("是否移除家庭成员XXX", (){});
                        },
                        child: Container(
                          padding: EdgeInsets.symmetric(horizontal: 25.w, vertical: 4),
                          decoration: BoxDecoration(
                            border: Border.all(color: Config.colorMain, width: 1.5.w),
                            borderRadius: BorderRadius.circular(Config.roundNormal.w),
                          ),
                          child: Text(
                            "移除",
                            style:
                            TextStyle(color: Config.colorMain, fontSize: Config.textSizeInfo.sp),
                          ),
                        ),
                      ),
                    ),
                  ],
                ))
              ],
            ),
          ),
          Visibility(
            visible: logic.familyMember.length > 1 && index < logic.familyMember.length - 1,
            child: CommonWidget.commonDivider(),
          ),
        ],
      ),
    );
  }
}
